﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;

namespace Web2014112110
{
    public partial class UpdateStuPhoto : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Title = "学生更换头像";
            string name;
            name = Session["name"].ToString();
            this.lblNowUser.Text = name;
            this.userName.Text = name;
            String strCn = "Server=pc.hstc.cn;database=dhd2014112110;uid=2014112110;pwd=19941225";
            SqlConnection cn = new SqlConnection(strCn); //建对象
            try
            {
                cn.Open();
                string strSql = "SELECT   Image FROM  Student  WHERE   (No = N'" + this.userName.Text + "')";
                SqlCommand cmd = new SqlCommand(strSql, cn);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    this.Image.ImageUrl = dr["Image"].ToString();
                }
            }
            catch
            {

            }
            finally
            {
                cn.Close();
            }
        }
        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (Session["Theme"] == null)
            {
                Page.Theme = "ThemeFour";
            }
            else if (Session["Theme"].ToString() == "themeFive")
            {
                Page.Theme = "themeFive";
            }
            else if (Session["Theme"].ToString() == "themeTwo")
            {
                Page.Theme = "ThemeTwo";
            }
            else if (Session["Theme"].ToString() == "themeThree")
            {
                Page.Theme = "themeThree";
            }
            else if (Session["Theme"].ToString() == "themeFour")
            {
                Page.Theme = "themeFour";
            }
        }
        protected void UpdateImage_Click(object sender, EventArgs e)
        {
            string FullFileName = flLl.PostedFile.FileName;
            string fileName = FullFileName.Substring(FullFileName.LastIndexOf("\\") + 1);
            //this.txtImage.Text = "~/photo/" + fileName;
            string type = fileName.Substring(fileName.LastIndexOf(".") + 1);
            if (type == "bmp" || type == "jpg" || type == "gif")
            {
                flLl.PostedFile.SaveAs(Server.MapPath("~/photo") + "\\" + fileName);
                Image.ImageUrl = "~/Photo/" + fileName;
                string strCn = "server=pc.hstc.cn;database=dhd2014112110;uid=2014112110;pwd=19941225";
                SqlConnection cn = new SqlConnection(strCn);
                try
                {
                    cn.Open();
                    string strSql = "UPDATE  Student SET Image = N'" + Image.ImageUrl + "' WHERE(No = N'" + this.userName.Text + "')";
                    SqlCommand cmd = new SqlCommand(strSql, cn);
                    int iUpd = cmd.ExecuteNonQuery();
                    if (iUpd < 1)
                    {
                        this.lblInfo.Text = "头像更换失败！";
                    }
                    else
                    {
                        this.lblInfo.Text = "头像更换成功";
                    }
                 }
                 catch (Exception ex)
                 {
                     this.lblInfo.Text = ex.Message;
                 }
                 finally
                 {
                    cn.Close();
                 }
            }
            else
            {
                this.lblInfo.Text ="您上传的格式有误，格式须为.bmp/.jpg/.gif。";
            }
            
        }
    }
}